package yao.config;

import org.dom4j.Element;

import yao.config.exception.ConfigException;
import yao.config.exception.ConfigSettingException;


/**
 * XML节点转换成原始节点对象
 * 
 * @author YaoMing
 */
final class Element2Node {

	/**
	 * 从Element中拿Node对象
	 * 
	 * @param e
	 * @return
	 * @throws ConfigException
	 * @throws ConfigSettingException
	 */
	static ENode element2Node(Element e) throws ConfigSettingException {
		ENode eNode = new ENode();

		eNode.name = e.attributeValue(PathContants.NAME);
		if (null == eNode.name || eNode.name.trim().equals(""))
			throw new ConfigSettingException("Name of node can't be null or empty");
		if (eNode.name.indexOf(".") != -1 || eNode.name.indexOf(" ") != -1) {
			throw new ConfigSettingException("Name of node can't include \".\" or \" \"");
		}

		Element valueE = e.element(PathContants.VALUE);
		if (null != valueE) {
			eNode.textValue = valueE.getText();
		}

		eNode.attributeValue = e.attributeValue(PathContants.VALUE);
		eNode.text = e.attributeValue(PathContants.TEXT);
		eNode.writeable = Boolean.parseBoolean(e.attributeValue(PathContants.WRITEABLE));
		eNode.valueMapElement = e.element(PathContants.VALUE_MAP);

		return eNode;
	}

}
